package manage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import core.constants.HttpConstants;
import core.domain.LoginUser;
import core.domain.Result;
import core.enums.ResultCode;
import core.enums.UserIdentity;
import core.exception.ServiceException;
import core.jwt.TokenService;
import manage.domain.sysuser.SysUser;
import core.domain.vo.LoginUserVO;
import manage.mapper.SysUserMapper;
import manage.service.ISysUserService;
import manage.utils.BCryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

@Service
@RefreshScope
public class SysUserServiceImpl implements ISysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Value("${jwt.secret}")
    private String secret;

    @Autowired
    private TokenService tokenService;
    @Override
    public Result<String> login(String username, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        SysUser user = sysUserMapper.selectOne(queryWrapper
                .select(SysUser::getId, SysUser::getPassword, SysUser::getNickname)
                .eq(SysUser::getUsername, username));
        if (user == null) {
            return Result.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if (!BCryptUtil.matches(password, user.getPassword())) {
            return Result.fail(ResultCode.FAILED_LOGIN);
        }
        // 登录成功
        // 生成token
        String token = tokenService.createJWT(user.getId(), secret, UserIdentity.ADMIN.getValue(), user.getNickname(), null);
        return Result.success(token);
    }

    @Override
    public int add(String username, String password) {
        SysUser user = sysUserMapper.selectOne(
                new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
        if (user != null) {
            throw new ServiceException(ResultCode.AILED_USER_EXISTS);
        }
        SysUser sysUser = new SysUser();
        sysUser.setUsername(username);
        sysUser.setPassword(BCryptUtil.encryptPassword(password));
        return sysUserMapper.insert(sysUser);
    }

    @Override
    public Result<LoginUserVO> info(String token) {
        if (StrUtil.isNotEmpty(token) &&
                token.startsWith(HttpConstants.PREFIX)) {
            token = token.replaceFirst(HttpConstants.PREFIX, StrUtil.EMPTY);
        }
        LoginUser loginUser = tokenService.getLoginUser(token, secret);
        if (loginUser == null) {
            return Result.fail(ResultCode.FAILED_UNAUTHORIZED);
        }
        LoginUserVO loginUserVO = new LoginUserVO();
        loginUserVO.setNickname(loginUser.getNickname());
//        loginUserVO.setIdentity(loginUser.getIdentity());
        return Result.success(loginUserVO);
    }

    @Override
    public boolean logout(String token) {
        if (StrUtil.isNotEmpty(token) &&
                token.startsWith(HttpConstants.PREFIX)) {
            token = token.replaceFirst(HttpConstants.PREFIX, StrUtil.EMPTY);
        }
        return tokenService.deleteLoginUser(token, secret);
    }

}
